package com.dcbeida.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.bean.NewPatient;
import com.dcbeida.listern.AlertDialogListener;
import com.dcbeida.listern.DialogCarmerListener;
import com.dcbeida.listern.DialogListener;

/**
 * Created by hp on 2016/9/10.
 */
public class DialogUtils {

    /**
     * 弹出选择照片弹窗
     *
     * @param listener
     */
    public static Dialog showDialog(Activity currActivity, final DialogCarmerListener listener) {

        final Dialog dialog = new Dialog(currActivity, R.style.myDialogTheme);
        LayoutInflater inflater = currActivity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_changehead, null);
        TextView tvCancl = (TextView) layout.findViewById(R.id.tvCancl);
        tvCancl.getPaint().setFakeBoldText(true);
        // 可指定对话框的背景色彩那些
        layout.findViewById(R.id.tvAlbum).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                listener.onAlbum();
            }
        });
        layout.findViewById(R.id.tvPhotograph).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                listener.onPhotograph();
            }
        });
        tvCancl.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }
        });

        dialog.setContentView(layout);

        // 设置对话框的出现位置，借助于window对象
        Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        // win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//
        // 弹出对话框时，底部窗体，不变暗。

        WindowManager.LayoutParams lp = win.getAttributes();
        // lp.x = -200;// x=0,y=0时，显示位置是屏幕中心。
        // lp.y = 0;
        // lp.alpha = 0.6f;// 对话框的透明度
        lp.width = android.view.WindowManager.LayoutParams.MATCH_PARENT;
        win.setAttributes(lp);
        //        win.setWindowAnimations(R.style.dialoganimstyle);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }


    /**
     * 弹出提示弹窗
     *
     * @param
     */
    public static void showDialogTip(Activity activity, int tip, String tip2,
                                     final DialogListener listener1, int canclStr, int confirmStr) {
        // if (dialog1 == null) {
        final Dialog dialog1 = new Dialog(activity, R.style.myDialogTheme);
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_tip, null);
        // 可指定对话框的背景色彩那些
        TextView tvTip = (TextView) layout.findViewById(R.id.tvTip);
        tvTip.setText(tip);
        TextView tvTip2 = (TextView) layout.findViewById(R.id.tvTip2);
        tvTip2.setText(tip2);
        TextView tvCancl = (TextView) layout.findViewById(R.id.tvCancl);
        tvCancl.setText(canclStr);
        TextView tvConfirm = (TextView) layout.findViewById(R.id.tvConfirm);
        tvConfirm.setText(confirmStr);
        tvCancl.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onCancl(dialog1);
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onConfirm(dialog1);
            }
        });
        dialog1.setContentView(layout);

        // 设置对话框的出现位置，借助于window对象
        Window win = dialog1.getWindow();
        win.setGravity(Gravity.CENTER);
        // win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//
        // 弹出对话框时，底部窗体，不变暗。

        WindowManager.LayoutParams lp = win.getAttributes();
        // lp.x = -200;// x=0,y=0时，显示位置是屏幕中心。
        // lp.y = 0;
        // lp.alpha = 0.6f;// 对话框的透明度
        if (MyApplication.widthPixels == 321) {
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            MyApplication.widthPixels = dm.widthPixels;
        }
        lp.width = (int) (MyApplication.widthPixels * 0.7);
        win.setAttributes(lp);
        dialog1.setCancelable(false);
        dialog1.show();

    }


    /**
     * 盒数提示
     *
     * @param
     */
    public static void showCountDialogTip(Activity activity, int a1, int a2,
                                          final DialogListener listener1) {
        // if (dialog1 == null) {
        final Dialog dialog1 = new Dialog(activity, R.style.myDialogTheme);
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_tip3, null);
        // 可指定对话框的背景色彩那些

        TextView tvTip2 = (TextView) layout.findViewById(R.id.tvTip2);
        tvTip2.setText(activity.getString(R.string.str118, a1+"", a2+""));
        TextView tvCancl = (TextView) layout.findViewById(R.id.tvCancl);
        TextView tvConfirm = (TextView) layout.findViewById(R.id.tvConfirm);
        tvCancl.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onCancl(dialog1);
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onConfirm(dialog1);
            }
        });
        dialog1.setContentView(layout);

        // 设置对话框的出现位置，借助于window对象
        Window win = dialog1.getWindow();
        win.setGravity(Gravity.CENTER);
        // win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//
        // 弹出对话框时，底部窗体，不变暗。

        WindowManager.LayoutParams lp = win.getAttributes();
        // lp.x = -200;// x=0,y=0时，显示位置是屏幕中心。
        // lp.y = 0;
        // lp.alpha = 0.6f;// 对话框的透明度
        if (MyApplication.widthPixels == 321) {
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            MyApplication.widthPixels = dm.widthPixels;
        }
        lp.width = (int) (MyApplication.widthPixels * 0.7);
        win.setAttributes(lp);
        dialog1.setCancelable(false);
        dialog1.show();

    }

    /**
     * 添加新患者
     *
     * @param
     */
    public static void showDialogAddNewPatient(Activity activity, int tip, final DialogListener listener1, int canclStr, int confirmStr) {
        // if (dialog1 == null) {
        final Dialog dialog1 = new Dialog(activity, R.style.myDialogTheme);
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_add_new_patient, null);
        // 可指定对话框的背景色彩那些
        TextView tvTip = (TextView) layout.findViewById(R.id.tvTip);
        tvTip.setText(tip);
        TextView tvCancl = (TextView) layout.findViewById(R.id.tvCancl);
        tvCancl.setText(canclStr);
        TextView tvConfirm = (TextView) layout.findViewById(R.id.tvConfirm);
        tvConfirm.setText(confirmStr);
        tvCancl.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onCancl(dialog1);
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onConfirm(dialog1);
            }
        });
        dialog1.setContentView(layout);

        // 设置对话框的出现位置，借助于window对象
        Window win = dialog1.getWindow();
        win.setGravity(Gravity.CENTER);
        // win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//
        // 弹出对话框时，底部窗体，不变暗。

        WindowManager.LayoutParams lp = win.getAttributes();
        // lp.x = -200;// x=0,y=0时，显示位置是屏幕中心。
        // lp.y = 0;
        // lp.alpha = 0.6f;// 对话框的透明度
        if (MyApplication.widthPixels == 321) {
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            MyApplication.widthPixels = dm.widthPixels;
        }
        lp.width = (int) (MyApplication.widthPixels * 0.7);
        win.setAttributes(lp);
        dialog1.setCancelable(false);
        dialog1.show();

    }

    /**
     * 添加新患者
     *
     * @param
     */
    public static void showSign(Activity activity, final DialogListener listener1) {
        // if (dialog1 == null) {
        final Dialog dialog1 = new Dialog(activity, R.style.myDialogTheme);
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_sign_tip1, null);
        // 可指定对话框的背景色彩那些
        TextView tvRefuse = (TextView) layout.findViewById(R.id.tv_refuse);


        TextView tvCompletion = (TextView) layout.findViewById(R.id.tv_completion);

        tvRefuse.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onCancl(dialog1);
            }
        });
        tvCompletion.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onConfirm(dialog1);
            }
        });
        dialog1.setContentView(layout);

        // 设置对话框的出现位置，借助于window对象
        Window win = dialog1.getWindow();
        win.setGravity(Gravity.CENTER);
        // win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//
        // 弹出对话框时，底部窗体，不变暗。

        WindowManager.LayoutParams lp = win.getAttributes();
        // lp.x = -200;// x=0,y=0时，显示位置是屏幕中心。
        // lp.y = 0;
        // lp.alpha = 0.6f;// 对话框的透明度
        if (MyApplication.widthPixels == 321) {
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            MyApplication.widthPixels = dm.widthPixels;
        }
        lp.width = (int) (MyApplication.widthPixels * 0.7);
        win.setAttributes(lp);
        dialog1.setCancelable(false);
        dialog1.show();

    }



    /**
     * 确定 取消提示
     *
     * @param
     */
    public static void showConfirmAndCancel(Activity activity, String tips, final DialogListener listener1, int canclStr, int confirmStr) {
        // if (dialog1 == null) {
        final Dialog dialog1 = new Dialog(activity, R.style.myDialogTheme);
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_add_new_patient, null);
        // 可指定对话框的背景色彩那些
        TextView tvTip = (TextView) layout.findViewById(R.id.tvTip);
        tvTip.setText(tips);
        TextView tvCancl = (TextView) layout.findViewById(R.id.tvCancl);
        tvCancl.setText(canclStr);
        TextView tvConfirm = (TextView) layout.findViewById(R.id.tvConfirm);
        tvConfirm.setText(confirmStr);
        tvCancl.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onCancl(dialog1);
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onConfirm(dialog1);
            }
        });
        dialog1.setContentView(layout);

        // 设置对话框的出现位置，借助于window对象
        Window win = dialog1.getWindow();
        win.setGravity(Gravity.CENTER);
        // win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//
        // 弹出对话框时，底部窗体，不变暗。

        WindowManager.LayoutParams lp = win.getAttributes();
        // lp.x = -200;// x=0,y=0时，显示位置是屏幕中心。
        // lp.y = 0;
        // lp.alpha = 0.6f;// 对话框的透明度
        if (MyApplication.widthPixels == 321) {
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            MyApplication.widthPixels = dm.widthPixels;
        }
        lp.width = (int) (MyApplication.widthPixels * 0.7);
        win.setAttributes(lp);
        dialog1.setCancelable(false);
        dialog1.show();

    }

    /**
     * 确定 取消提示
     *
     * @param
     */
    public static void showConfirm(Activity activity, String tips, final DialogListener listener1,  int confirmStr) {
        // if (dialog1 == null) {
        final Dialog dialog1 = new Dialog(activity, R.style.myDialogTheme);
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_confirm, null);
        // 可指定对话框的背景色彩那些
        TextView tvTip = (TextView) layout.findViewById(R.id.tvTip);
        tvTip.setText(tips);

        TextView tvConfirm = (TextView) layout.findViewById(R.id.tvConfirm);
        tvConfirm.setText(confirmStr);
        tvConfirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                listener1.onConfirm(dialog1);
            }
        });
        dialog1.setContentView(layout);

        // 设置对话框的出现位置，借助于window对象
        Window win = dialog1.getWindow();
        win.setGravity(Gravity.CENTER);
        // win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//
        // 弹出对话框时，底部窗体，不变暗。

        WindowManager.LayoutParams lp = win.getAttributes();
        // lp.x = -200;// x=0,y=0时，显示位置是屏幕中心。
        // lp.y = 0;
        // lp.alpha = 0.6f;// 对话框的透明度
        if (MyApplication.widthPixels == 321) {
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            MyApplication.widthPixels = dm.widthPixels;
        }
        lp.width = (int) (MyApplication.widthPixels * 0.7);
        win.setAttributes(lp);
        dialog1.setCancelable(false);
        dialog1.show();

    }



    /**
     * 添加新患者
     *
     * @param
     */
    public static void showDialogApplyReturn(Activity activity, String box, String money, final DialogListener listener1, int canclStr, int confirmStr) {
        // if (dialog1 == null) {
        final Dialog dialog1 = new Dialog(activity, R.style.myDialogTheme);
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_add_apply_return, null);
        // 可指定对话框的背景色彩那些
        TextView tv_box = (TextView) layout.findViewById(R.id.tv_box);
        tv_box.setText(box);
        TextView tv_money = (TextView) layout.findViewById(R.id.tv_money);
        tv_money.setText(money);
        TextView tvCancl = (TextView) layout.findViewById(R.id.tvCancl);
        tvCancl.setText(canclStr);
        TextView tvConfirm = (TextView) layout.findViewById(R.id.tvConfirm);
        tvConfirm.setText(confirmStr);
        tvCancl.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                listener1.onCancl(dialog1);
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                listener1.onConfirm(dialog1);
            }
        });
        dialog1.setContentView(layout);

        // 设置对话框的出现位置，借助于window对象
        Window win = dialog1.getWindow();
        win.setGravity(Gravity.CENTER);
        // win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//
        // 弹出对话框时，底部窗体，不变暗。

        WindowManager.LayoutParams lp = win.getAttributes();
        // lp.x = -200;// x=0,y=0时，显示位置是屏幕中心。
        // lp.y = 0;
        // lp.alpha = 0.6f;// 对话框的透明度
        if (MyApplication.widthPixels == 321) {
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            MyApplication.widthPixels = dm.widthPixels;
        }
        lp.width = (int) (MyApplication.widthPixels * 0.7);
        win.setAttributes(lp);
        dialog1.setCancelable(false);
        dialog1.show();

    }


    /**
     * 系统的dialog
     *
     * @param activity
     * @param title
     * @param msg
     * @param confirm
     * @param cancle
     * @param listener
     */
    public static void showAlertDialog(Activity activity, String title, String msg, String confirm, String cancle, final AlertDialogListener listener) {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(activity);
//        normalDialog.setIcon(R.drawable.icon_dialog);
        normalDialog.setTitle(title);
        normalDialog.setMessage(msg);
        normalDialog.setPositiveButton(confirm,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        listener.onConfirm(dialog, which);
                    }
                });
        normalDialog.setNegativeButton(cancle,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.onCancl(dialog, which);
                    }
                });
        // 显示
        normalDialog.setCancelable(false);
        normalDialog.show();
    }


}
